package org.atovio.coolexam.service.impl;

import org.atovio.coolexam.entity.ExamPaper;
import org.atovio.coolexam.exception.ServiceException;
import org.atovio.coolexam.mapper.ExamPaperMapper;
import org.atovio.coolexam.service.ExamPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ExamPaperServiceImpl implements ExamPaperService {

    private final ExamPaperMapper examPaperMapper;

    @Autowired
    public ExamPaperServiceImpl(ExamPaperMapper examPaperMapper) {
        this.examPaperMapper = examPaperMapper;
    }

    @Override
    public int getCount(Map<String, Object> criteria) {
        return examPaperMapper.selectCountSelective(criteria);
    }

    @Override
    public ExamPaper add(ExamPaper examPaper) {
        int count = examPaperMapper.insert(examPaper);
        if (count != 1)
            throw new ServiceException("数据库错误");
        return examPaperMapper.selectByName(examPaper.getName());
    }

    @Override
    public List<ExamPaper> getAll() {
        return examPaperMapper.selectAll();
    }

    @Override
    public void deleteById(int id) {
        int count = examPaperMapper.deleteById(id);
        if (count !=1)
            throw new ServiceException("数据库内部错误");
    }

    @Override
    public List<ExamPaper> getSelective(Map<String, Object> criteria) {
        return examPaperMapper.selectSelective(criteria);
    }

    @Override
    public ExamPaper getById(int id) {
        return examPaperMapper.selectById(id);
    }
}
